package yxy.game.pm2.command.friend;

import yxy.cherry.data.bean.friendsdate;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FriendMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.friend.FriendHandle;
import yxy.game.pm2.module.friend.FriendModule;
import yxy.game.pm2.module.task.TaskHandle;

/** 好友 赠送友情点 */
@MessageType(proto = Command.FriendPoint, classtype = FriendMessage.FriendPointRequest.class)
public class CMD_FriendPoint extends Command<FriendMessage.FriendPointRequest> {

	@Override
	public void execute(MessageSource messageSource, FriendMessage.FriendPointRequest request) {
		String playerCode = messageSource.getTag();
		String friendCode = request.getPlayerCode();

		FriendMessage.FriendPointResponse.Builder response = FriendMessage.FriendPointResponse.newBuilder();

		FriendModule playerModule = FriendModule.get(playerCode);
		FriendModule friendModule = FriendModule.get(friendCode);
		friendsdate data = playerModule.data();
		CurrencyHandle currencyHandle = new CurrencyHandle();
		try {
			if (friendModule.tryLock() && playerModule.tryLock()) {
				// 赠送上限
				if (playerModule.getGiven().size() >= data.getUplimit()) {
					send(messageSource, response, Result.FriendPointNoTimes);// 你今天的赠送次数已达上限
					return;
				}
				if (!playerModule.getFriend().contains(friendCode)) {
					send(messageSource, response, Result.FriendPointCantGiven);// 这个玩家不能赠送
					return;
				}
				if (playerModule.getGiven().contains(friendCode)) {
					send(messageSource, response, Result.FriendPointHasGiven);// 这个玩家已经赠送过了
					return;
				}
				// 奖励
				Currency reward = Currency.create(data.getGivegettype(), data.getGivegetid(), data.getGivegetnum());
				currencyHandle.addCurrency(playerCode, CCReason.好友, reward);

				playerModule.getGiven().add(friendCode);
				friendModule.getGiftn().add(playerCode);

				playerModule.set();
				friendModule.set();

				FriendMessage.Friend friend = new FriendHandle().packFriend(playerModule, friendCode);

				response.setFriend(friend);
				response.addRewards(currencyHandle.packItem(reward));

				send(messageSource, response, Result.Success);

				TaskHandle.handle(playerCode, TaskType.统计型_好友_赠送_次数, 1);
				TaskHandle.handle(playerCode, TaskType.统计型_好友_赠送_累计_达到, 1);
			}
		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);
		} finally {
			friendModule.unlock();
			playerModule.unlock();
		}
	}

	private void send(MessageSource messageSource, FriendMessage.FriendPointResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}